//
//  FindDetailViewController.m
//  geiTA
//
//  Created by 曲丹阳 on 14-11-21.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FindDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "UIScrollView+TwitterCover.h"
#import "FindSixModel.h"

@interface FindDetailViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@end

@implementation FindDetailViewController
- (void)dealloc
{
    [_web release];
    [_image release];
    [_scrollview release];
    [_label release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self.navigationController.navigationBar setTranslucent:YES];
    self.navigationController.tabBarController.tabBar.hidden = YES;
    
    [self.image setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.image setImageWithURL:[NSURL URLWithString:_model.cover_image_url]];

    [self.label setFrame:CGRectMake(self.view.frame.size.width / 10, CHTwitterCoverViewHeight / 2.5f, self.view.frame.size.width / 10 * 8, 50)];
    [self.label setFont:[UIFont boldSystemFontOfSize:19.0]];
    [self.label setTextAlignment:NSTextAlignmentCenter];
    [self.label setNumberOfLines:0];
    [self.label setTextColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8]];
    [self.label setText:_model.title];
    
    
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, CHTwitterCoverViewHeight, self.view.frame.size.width, self.view.frame.size.height)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_model.content_url]];
    [_web loadRequest:request];
    
    _web.scrollView.scrollEnabled = NO; //
    _web.delegate = self;
    
    self.scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [_scrollview setContentSize:_web.scrollView.contentSize];
    
    [_scrollview addTwitterCoverWithImage:_image.image];
    [_scrollview addSubview:_web];
    [_scrollview addSubview:_label];
    [self.view addSubview:_scrollview];
    
    [_scrollview release];
    [_web release];
    [_image release];
    
    [self creatLeftButton];  //返回按钮
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.image = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.label = [[UILabel alloc] initWithFrame:CGRectZero];
        self.model = [[FindSixModel alloc] init];
    }
    return self;
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    // 加载完成 改变滚动范围
    //NSLog(@"%f", webView.scrollView.contentSize.height);
    [self.web setFrame:CGRectMake(0, CHTwitterCoverViewHeight, self.view.frame.size.width, webView.scrollView.contentSize.height + 113)];
    [self.scrollview setContentSize:webView.scrollView.contentSize];
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%@", error);
}

#pragma mark -方法传值
- (void)getdetailurlModel:(FindSixModel *)model
{
    self.model = model;
    
}

#pragma mark -返回按钮
- (void)creatLeftButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:left];
    
}

- (void)leftButtonAction:(id)sender
{
    if ([_web canGoBack]) {
        [_web goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
